package com.example.demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

class StudentAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnChanceListen listen;

    public StudentAdapter(Context context, ArrayList<Student> studentArrayList, IOnChanceListen listen) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listen = listen;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return studentArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView ==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);

            StuViewHolder stuViewHolder = new StuViewHolder();
            stuViewHolder.tvStuno = convertView.findViewById(R.id.txt_stuno);
            stuViewHolder.tvName  = convertView.findViewById(R.id.txt_stuname);
            stuViewHolder.tvAge   = convertView.findViewById(R.id.txt_stuage);
            stuViewHolder.ivDel   = convertView.findViewById(R.id.iv_delect);

            convertView.setTag(stuViewHolder);
        }
        final Student student = studentArrayList.get(position);
        StuViewHolder viewHolder = (StuViewHolder) convertView.getTag();
        viewHolder.tvStuno.setText(student.getStuNo());
        viewHolder.tvName.setText(student.getStuName());
        viewHolder.tvAge.setText(String.valueOf(student.getStuAge()));
        viewHolder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listen.delect(student);
            }
        });
        return convertView;


    }
}
